package com.example.test.login;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.test.R;
import com.example.test.all_Myhelper.MyHelper;
import com.example.test.bottom_tab.tab_main_activity;
import com.example.test.exam.Main;

public class login_main_activity extends AppCompatActivity implements View.OnClickListener{

    MyHelper myHelper;
    public Button lg_btn;
    public EditText lg_id,lg_passwd;
    public TextView lg_register,lg_title;
    public ImageView lg_imageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        myHelper = new MyHelper(this);
        init();
//        Bundle bundle1 = getIntent().getExtras();
//        String id = bundle1.getString("id");
//        lg_id.setText(String.valueOf(id));
    }
    private void init() {
        lg_btn=(Button) findViewById(R.id.lg_btn);
        lg_id=(EditText) findViewById(R.id.lg_id);
        lg_passwd=(EditText) findViewById(R.id.lg_passwd);
        lg_register = (TextView) findViewById(R.id.lg_register);
        lg_title = (TextView) findViewById(R.id.lg_title);
        lg_imageView = (ImageView) findViewById(R.id.lg_imageView);
        lg_imageView.setBackgroundResource(R.drawable.loginin);
        lg_btn.setOnClickListener(this);
        lg_id.setOnClickListener(this);
        lg_passwd.setOnClickListener(this);
        lg_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String  password;
        SQLiteDatabase db;
        ContentValues values;
        if(v.getId()==R.id.lg_btn){
                db = myHelper.getReadableDatabase();
                System.out.println(lg_id.getText().toString());
                Cursor cursor = db.rawQuery("select * from user where name=? and password=?",new String[]{lg_id.getText().toString(),lg_passwd.getText().toString()});
                Cursor ad = db.rawQuery("select * from user where name = 'admin' and password = '123' ",null);
                if (cursor.getCount() == 0) {
                    AlertDialog aldg;
                    AlertDialog.Builder adbd = new AlertDialog.Builder(login_main_activity.this);
                    adbd.setTitle("提示");
                    adbd.setMessage("用户不存在或者密码错误");
                    adbd.setNegativeButton("返回", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    aldg = adbd.create();
                    aldg.show();
                }else if(ad.getCount()>0 && lg_id.getText().toString().equals("admin") && lg_passwd.getText().toString().equals("123")){
                    Intent ad_initent = new Intent(login_main_activity.this, tab_main_activity.class);
                    startActivity(ad_initent);
                }else{
//                    while(cursor.moveToNext()){
//                        String name = cursor.getString(cursor.getColumnIndex("name"));
//                        String pass = cursor.getString(cursor.getColumnIndex("password"));
//                        System.out.println(name+"   "+ pass+"  "+cursor.getCount());
//                    }
                    Intent dl_intent = new Intent(login_main_activity.this, Main.class);
                    startActivity(dl_intent);

                }
                cursor.close();
                ad.close();
                db.close();
        }else if(v.getId()==R.id.lg_register){
             Intent lg_to_r_intent = new Intent(login_main_activity.this,register.class);
             startActivity(lg_to_r_intent);
        }

    }
}


